package com.hw.hdfs.controller;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.hw.hdfs.service.HDFSService;

@Controller
public class HDFSController {

	private static Logger log=LoggerFactory.getLogger(HDFSController.class);
	
	@Autowired
	private HDFSService hservice;
	
	@RequestMapping(value="/",headers="charset=UTF-8")
//	produces="application/json;charset=UTF-8"
	public String index(){
		return "index";
	}
	
	@RequestMapping("/mkdir")
	@ResponseBody
	public boolean mkdir(String path) throws Exception{
		
		return hservice.mkdir(path);
	}
	@RequestMapping("/localToHdfs")
	@ResponseBody
	public boolean localToHdfs(String localPath,String hdfsPath) throws Exception{
//		tomcat中的新特性：就是严格按照 RFC 3986规范进行访问解析，
//		而 RFC 3986规范定义了Url中只允许包含英文字母（a-zA-Z）、数字（0-9）、-_.~4个特殊字符以及
//		所有保留字符(RFC3986中指定了以下字符为保留字符：! * ’ ( ) ; : @ & = + $ , / ? # [ ])
//		String srcPath=disk+":\\"+localPath;
		return hservice.localToHdfs(localPath, hdfsPath);
	}
	
	@RequestMapping("/hdfsToLocal")
	@ResponseBody
	public boolean hdfsToLocal(String hdfsPath,String localPath) throws Exception{
//		String dstPath=disk+":\\"+localPath;
		return hservice.HdfsToLocal(hdfsPath, localPath);
	}
	@RequestMapping("/uploadFile")
	@ResponseBody
	public boolean uploadFile(String hdfsPath,MultipartFile file) throws Exception{
		
		return hservice.uploadFile(hdfsPath, file);
	}
	
	@RequestMapping("/downloadFile")
	@ResponseBody
	public boolean downloadFile(String hdfsPath,String downloadPath) throws Exception{
		
		return hservice.downLoadFile(hdfsPath, downloadPath);
	}
	
	
	@RequestMapping("/files")
	@ResponseBody
	public List<Map<String, String>> getFiles(String path) throws Exception{
		
		return hservice.listFile(path);
	}
	@RequestMapping("/pathList")
	@ResponseBody
	public List<Map<String, Object>> getPathList(String path) throws Exception{
		
		return hservice.getPathList(path);
	}
	@RequestMapping("/getFile")
	@ResponseBody
	public String getFile(String path) throws Exception{
		
		return hservice.readFile(path);
	}
	@RequestMapping("/delFile")
	@ResponseBody
	public boolean deleteFile(String path) throws Exception{
		
		return hservice.deleteFile(path);
	}
    
}